package gui;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class CharCreateWindow extends JDialog implements ActionListener 
{
	private static final long serialVersionUID = 3049538331297083426L;
	
	private JLabel jl_name = new JLabel("Name:");
	private JLabel jl_class = new JLabel("Class:");
	private JLabel jl_race = new JLabel("Race:");
	private JLabel jl_player = new JLabel("Player:");
	private JTextField jtf_name = new JTextField();
	private JTextField jtf_class = new JTextField();
	private JTextField jtf_race = new JTextField();
	private JTextField jtf_player = new JTextField();
	private JButton jb_create = new JButton("Create");
	private JButton jb_cancel = new JButton("Cancel");
	
	private GridBagConstraints gbc_main = new GridBagConstraints();
	
	protected transient String[] sa_input;
	
	public CharCreateWindow()
	{
		setLayout(new GridBagLayout());
		setTitle("New Character");
		
		jl_name.setFocusable(false);
		jl_class.setFocusable(false);
		jl_race.setFocusable(false);
		jl_player.setFocusable(false);
		
		jb_create.addActionListener(this);
		jb_cancel.addActionListener(this);
		
		gbc_main.fill = GridBagConstraints.HORIZONTAL;
		gbc_main.weightx = 1.0;
		gbc_main.weighty = 1.0;
		gbc_main.gridwidth = 1;
		gbc_main.gridheight = 1;
		gbc_main.insets = new Insets(3, 3, 3, 3);
		
		/* Add labels to this */
		
		//add jl_name to this
		gbc_main.gridx = 0;
		gbc_main.gridy = 0;
		add(jl_name, gbc_main);
		
		//add jl_class to this
		gbc_main.gridx = 0;
		gbc_main.gridy = 1;
		add(jl_class, gbc_main);
		
		//add jl_race to this
		gbc_main.gridx = 0;
		gbc_main.gridy = 2;
		add(jl_race, gbc_main);
		
		//add jl_player to this
		gbc_main.gridx = 0;
		gbc_main.gridy = 3;
		add(jl_player, gbc_main);
		
		/* Add text fields to this */
		
		//add jtf_name to this
		gbc_main.gridx = 1;
		gbc_main.gridy = 0;
		add(jtf_name, gbc_main);
		
		//add jtf_class to this
		gbc_main.gridx = 1;
		gbc_main.gridy = 1;
		add(jtf_class, gbc_main);
		
		//add jtf_race to this
		gbc_main.gridx = 1;
		gbc_main.gridy = 2;
		add(jtf_race, gbc_main);
		
		//add jtf_player to this
		gbc_main.gridx = 1;
		gbc_main.gridy = 3;
		add(jtf_player, gbc_main);
		
		/* Add buttons to this */
		
		gbc_main.fill = GridBagConstraints.NONE;
		
		//add jb_create to this
		gbc_main.gridx = 0;
		gbc_main.gridy = 4;
		add(jb_create, gbc_main);
		
		//add jb_cancel to this
		gbc_main.gridx = 1;
		gbc_main.gridy = 4;
		add(jb_cancel, gbc_main);
		
		pack();
		GUIHelper.Center(this);
		setModal(true);
		setVisible(true);
	}
	
	public String[] getInput(){ return sa_input; }

	public void actionPerformed(ActionEvent event)
	{
		if(event.getSource().equals(jb_cancel)) dispose();
		
		if(event.getSource().equals(jb_create))
		{
			sa_input = new String[4];
			
			sa_input[0] = jtf_name.getText();
			sa_input[1] = jtf_class.getText();
			sa_input[2] = jtf_race.getText();
			sa_input[3] = jtf_player.getText();
			
			if(sa_input[0] == null || sa_input[0].isEmpty()
					|| sa_input[1] == null || sa_input[1].isEmpty()
					|| sa_input[2] == null || sa_input[2].isEmpty()
					|| sa_input[3] == null || sa_input[3].isEmpty())
				sa_input = null;
		}
	}
	
	public static String[] newChar()
	{ 		
		
		CharCreateWindow ccw_create = new CharCreateWindow();	
		String[] sa_char_data = ccw_create.getInput();
		
		return sa_char_data; 
	}
}
